#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>
#include <libavutil/time.h>
#include <alsa/asoundlib.h>
#include "Log.h"
#include "MediaPlayer.h"


MediaPlayer *player;

void display_avpara()
{
	/*音视频参数*/
	LOGD("");

	LOGD("duration:%ds", player->getPropertyInt(Player_Property_Int_Duration));

	LOGD("playbackrate:%0.2f", player->getPropertyFloat(Player_Property_Float_PlayBackRate));
	LOGD("avdiff:%0.2f", player->getPropertyFloat(Player_Property_Float_AVDiff));
	LOGD("droprate:%0.2f", player->getPropertyFloat(Player_Property_Float_DropFrameRate));
	LOGD("fps:%0.2f", player->getPropertyFloat(Player_Property_Float_FPS));
	LOGD("volume:%d", player->getVolume());

	LOGD("videodecoder:%d", player->getPropertyInt(Player_Property_Int_VideoDecoder));
	LOGD("videoduration:%d", player->getPropertyInt(Player_Property_Int_VideoCachedDuration));
	LOGD("videobytes:%d", player->getPropertyInt(Player_Property_Int_VideoCachedBytes));
	LOGD("videopackets:%d", player->getPropertyInt(Player_Property_Int_VideoCachedPackets));
	LOGD("videoheight:%d", player->getPropertyInt(Player_Property_Int_VideoHeight));
	LOGD("videowidth:%d", player->getPropertyInt(Player_Property_Int_VideoWidth));
	LOGD("videoformat:%d", player->getPropertyInt(Player_Property_Int_VideoFormat));
	LOGD("videobitrate:%d", player->getPropertyInt(Player_Property_Int_VideoBitRate));

	LOGD("audiostream:%d", player->getPropertyInt(Player_Property_Int_AudioStream));
	LOGD("audiodecoder:%d", player->getPropertyInt(Player_Property_Int_AudioDecoder));
	LOGD("audioduration:%d", player->getPropertyInt(Player_Property_Int_AudioCachedDuration));
	LOGD("audiobytes:%d", player->getPropertyInt(Player_Property_Int_AudioCachedBytes));
	LOGD("audiopackets:%d", player->getPropertyInt(Player_Property_Int_AudioCachedPackets));
	LOGD("audiosamplesize:%d", player->getPropertyInt(Player_Property_Int_AudioFrameSize));
	LOGD("audiosamplerate:%d", player->getPropertyInt(Player_Property_Int_AudioSampleRate));
	LOGD("audiochannels:%d", player->getPropertyInt(Player_Property_Int_AudioChannels));

	LOGD("audiochannellayout:%d", player->getPropertyInt(Player_Property_Int_AudioChannelLayout));
	LOGD("audioformat:%d", player->getPropertyInt(Player_Property_Int_AudioFormat));
	LOGD("audiobitrate:%d", player->getPropertyInt(Player_Property_Int_AudioBitRate));

	LOGD("");
}

void event_callback(Message *msg, void *ctx)
{
	MediaPlayer *mplayer = (MediaPlayer *)ctx;

	//prepared 音视频信息、解码线程都已经准备完成
	if(Player_Evt_PrePared == msg->what) {
		display_avpara();
	}

	if(Player_Evt_Completed == msg->what) {
		mplayer->start();
	}
}

void stop(int signo)
{
	LOGD("Ctrl+C Press! exit");
	if(player) {
		player->stop();
		delete player;
	}
    exit(0);
}

int main(int argc, char *argv[])
{
	struct sigaction sigIntHandler;
    sigIntHandler.sa_handler = stop;
    sigemptyset(&sigIntHandler.sa_mask);
    sigIntHandler.sa_flags = 0;
    sigaction(SIGINT, &sigIntHandler, NULL);

    Log.init();
    Log.setLevel(SPDLOG_LEVEL_DEBUG);
    Log.loadEnvLevel();
   
    if(argc != 3) {
    	LOGD("Usage:%s url volume(0-100)", argv[0]);
    	return 0;
    }

	player = new MediaPlayer();

	/*事件通知回调注册*/ 
	player->registerEventCB(event_callback, player);

	// player->setOption("keep-video-ratio", 1);
	player->setVolume(atoi(argv[2]));

	player->setDataSource(argv[1]);
	//异步播放
	player->prepareASync();

#ifdef RK
	while(1)
		sleep(1);
#else
	SDLVideoSink::eventLoop(player);
#endif

	player->stop();

	delete player;
	return 0;
}